package com.dataWarehouseOss.rabbitmq.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author ：LiuShihao
 * @date ：Created in 2020/8/18 2:42 下午
 * @desc ：
 */
@Slf4j
public class DelFileUtil {

    public static FTPClient ftp;
//    public static final String FTP_IP = "192.168.19.13";
//    public static final Integer FTP_PORT = 21;
//    public static final String FTP_USERNAME = "pengxiaoyang";
//    public static final String FTP_PASSWORD = "pxy19840208";
//    public static final String LOCAL_PATH = "/data/hw_data";
//    public static final String FTP_PATH = "/lshrabbitMQ/hw_data";

    @Value("${ftp.ip}")
    public static String FTP_IP ;
    @Value("${ftp.ftp_port}")
    public  static Integer FTP_PORT ;
    @Value("${ftp.username}")
    public  static String FTP_USERNAME ;
    @Value("${ftp.password}")
    public  static String FTP_PASSWORD ;
    @Value("${local.path}")
    public  static String LOCAL_PATH  ;
    @Value("${ftp.ftpPath}")
    public static  String FTP_PATH ;

    /**
     * 递归删除文件夹及文件
     * @param dir
     */
    public static void remove(File dir) {
        File files[] = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            if(files[i].isDirectory()) {
                remove(files[i]);
            }else {
                //删除文件
                log.info("删除文件  ::  "+files[i].toString());
                files[i].delete();
            }
        }
        //删除目录
        dir.delete();
        log.info("删除目录  ::  "+dir.toString());
    }

    /**
     * 删除Linux两天以前的文件
     */
    public static void deltwoDaysAgoFile(){

        String twoDaysAgo = LocalDateTime.now().minusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String regex = twoDaysAgo+".*";
        File file = new File(LOCAL_PATH);
//        if (!file.exists()){
//            log.info("目录不存在，正在创建...");
//            file.mkdirs();
//        }
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.getName().matches(regex)){
                boolean delete = file1.delete();
                if (delete){
                    log.info(file1.getName()+" 本地文件删除成功~");
                } else {
                    log.info(file1.getName()+" 本地文件删除失败~");
                }
            }
        }
    }

    /**
     * 要切换ftp的工作目录才能删除！ftp.changeWorkingDirectory("/lshrabbitMQ/hw_data");
     * @throws IOException
     */
    public static void delFTPFile() throws IOException {
        ftp = new FTPClient();
        ftp.connect(FTP_IP, FTP_PORT);
        ftp.login(FTP_USERNAME, FTP_PASSWORD);
        if(FTPReply.isPositiveCompletion(ftp.getReplyCode())){
            log.info("连接成功");
        }else{
            log.info("连接失败");
        }
        String twoDaysAgo = LocalDateTime.now().minusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String regex = twoDaysAgo+".*";
        FTPFile[] ftpFiles = ftp.listFiles(FTP_PATH);
        for (FTPFile ftpFile : ftpFiles) {
            if (ftpFile.getName().matches(regex)){
                ftp.changeWorkingDirectory(FTP_PATH);
                boolean bool = ftp.deleteFile(ftpFile.getName());
                if (bool){
                    log.info(ftpFile.getName()+" ftp文件删除成功！");
                }else {
                    log.info(ftpFile.getName()+" ftp文件删除失败！");
                }
            }
        }
    }
}
